package com.czk.gulimall.seckill.scheduled;

import com.czk.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2022/6/14 21:59
 */
@Slf4j
@Component
public class SeckillSkuScheduled {

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private SeckillService sekillService;
    private static final String upload_lock = "seckill:upload:lock";

    /**
     * 每天晚上三点，上架最近三天需要秒杀的商品
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void uploadSeckill() {

        // 增加分布式锁
        RLock lock = redissonClient.getLock(upload_lock);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            sekillService.uploadSeckill();
        } finally {
            lock.unlock();
        }


    }

}
